package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.Department;
import ywpt.model.UnitInfo;
import ywpt.web.common.PublicFunction;
@Service("unitInfoService")
@RemoteProxy(name="unitInfoService")
public class UnitInfoServiceImpl extends HibernateEntityDao<UnitInfo> implements  UnitInfoService {
	public  UnitInfoServiceImpl (){
		
	}

	public UnitInfo getUnitInfoById(Long id) {
		// TODO 自动生成方法存根
		return this.get(id);
	}

	public int getUnitInfoCountByCriteria(DetachedCriteria query) {
		// TODO 自动生成方法存根
		return this.getCountByCriteria(query);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<UnitInfo> getUnitInfos(DetachedCriteria query, int pageSize, int startIndex) {
		// TODO 自动生成方法存根
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	public List<UnitInfo> getUnitInfos(DetachedCriteria query) {
		// TODO 自动生成方法存根
		return this.findAllByCriteria(query);
	}

	public List<UnitInfo> getUnitInfos() {
		// TODO 自动生成方法存根
		return this.getAll();
	}
  @RemoteMethod
	public List<UnitInfo> getUnitInfosByUnitName(String unitName, int start, int size) {
		// TODO 自动生成方法存根

		DetachedCriteria query = DetachedCriteria.forClass(UnitInfo.class, "unitinfo");

		if ( unitName != null && !unitName.trim().equals("") )
		{
			query.add((Restrictions.like("unit_name", "%" + unitName + "%" ) ));
				
		}
		query.addOrder(Order.asc("id"));
		List<UnitInfo> unitInfoList = new ArrayList<UnitInfo>();


		
		unitInfoList.addAll(this.findAllByCriteria(query, size, start));

		return unitInfoList;
	}
  @RemoteMethod
	public int getUnitInfosCountByName(String unitName) {
		// TODO 自动生成方法存根
		DetachedCriteria query = DetachedCriteria.forClass(UnitInfo.class, "unitinfo");

		if ( unitName != null && !unitName.trim().equals("") )
		{
			query.add((Restrictions.like("unit_name", "%" + unitName + "%" )));
				
		}
		query.setProjection(null);
		return 	this.getCountByCriteria(query);
	}
	@RemoteMethod
	public List<UnitInfo> getUnitsByIds(String ids) {
		// TODO 自动生成方法存根
		if(ids.length()>0)
			ids=ids.substring(0,ids.length()-1);
		String idstr[]=ids.split(",");
		Long id[]=new Long[idstr.length];
		for(int i=0;i<idstr.length;i++)
			id[i]=Long.parseLong(idstr[i]);
		DetachedCriteria query=DetachedCriteria.forClass(UnitInfo.class);
		query.add(Restrictions.in("id",id));
		List<UnitInfo> list=this.findAllByCriteria(query);
		return list;
	}

	public void removeUnitInfo(Long id) {
		
		// TODO 自动生成方法存根
		UnitInfo unitInfo = this.get(id);
		
		unitInfo.setDelete_flag(true);
		this.update(unitInfo);

	}

	public void saveUnitInfo(UnitInfo unitInfo) {
		// TODO 自动生成方法存根
		this.save(unitInfo);
	}

	public void updateUnitInfo(UnitInfo unitInfo) {
		// TODO 自动生成方法存根
		this.update(unitInfo);
	}




}
